package com.quan.Routting;

import com.quan.Config.MQConnection;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;

public class Publisher {


    @Test
    public void publisher() throws Exception{
        //创建连接
        Connection conn = MQConnection.getRabbitmaConection();

        //创建channel
        Channel channel = conn.createChannel();

        //创建exchange，绑定队列
        channel.exchangeDeclare("routing-exchange", BuiltinExchangeType.DIRECT);
        channel.queueBind("routing-queue-error","routing-exchange","Error");
        channel.queueBind("routing-queue-info","routing-exchange","INFO");

        //发布消息到exchange，同时指定路由规则。
        channel.basicPublish("routing-exchange","Error",null,"ERROR".getBytes());
        channel.basicPublish("routing-exchange","INFO",null,"INFO1".getBytes());
        channel.basicPublish("routing-exchange","INFO",null,"INFO2".getBytes());
        channel.basicPublish("routing-exchange","INFO",null,"INFO3".getBytes());


        System.out.println("生产者发布消息成功");

        //释放资源
        channel.close();
        conn.close();

    }
}
